package com.okx.bot.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.okx.bot.dto.LineData;
import com.okx.bot.dto.Prod;
import com.okx.bot.dto.ProdData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class OkxService {

    @Autowired
    private OkxApiClient okxApiClient;
    @Value("${okx.white-inst-ids}")
    private List<String> whiteInstIds;
    private final ObjectMapper objectMapper = new ObjectMapper();

    public List<String> getAllProd() {
        List<String> result = new ArrayList<>();
        String allProd = okxApiClient.getAllProd();
        try {
            ProdData prodData = objectMapper.readValue(allProd, ProdData.class);
            List<Prod> prodList = prodData.getData();
            prodList.stream().forEach(p -> {
                if ("USDT".equals(p.getSettleCcy()) && !whiteInstIds.contains(p.getInstId())) {
                    result.add(p.getInstId());
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    // 筛选5日平均成交了大于3000W
    public List<String> getHighVolumeContracts(List<String> instIds, double volumeThreshold) {
        List<String> highVolumeContracts = new ArrayList<>();
        for (String instId : instIds) {
            String lineData = okxApiClient.getLineData(instId, "1D", 6);
            try {
                LineData kline = objectMapper.readValue(lineData, LineData.class);
                if (kline.getData() != null && kline.getData().size() == 6) {
                    double totalVolume = 0;
                    for (List<String> candle : kline.getData()) {
                        if ("1".equals(candle.get(8))) {
                            totalVolume += Double.parseDouble(candle.get(7)); // 成交量在K线数据的第6个位置
                        }
                    }
                    double averageVolume = totalVolume / 5;
                    if (averageVolume > volumeThreshold) {
                        highVolumeContracts.add(instId);
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return highVolumeContracts;
    }

    public List<String> executeVolStrategy(List<String> instIds) {
        List<String> result = new ArrayList<>();
        for (String instId : instIds) {
            String lineData = okxApiClient.getLineData(instId, "5m", 11);
            try {
                LineData kline = objectMapper.readValue(lineData, LineData.class);
                if (kline.getData() != null && kline.getData().size() == 11) {
                    double totalVolume = 0.0;
                    for (int i = 1; i < kline.getData().size(); i++) {
                        totalVolume += Double.parseDouble(kline.getData().get(i).get(7)); // 成交量在K线数据的第6个位置
                    }
                    double curVol = Double.parseDouble(kline.getData().get(0).get(7));
                    double high = Double.parseDouble(kline.getData().get(0).get(2));
                    double low = Double.parseDouble(kline.getData().get(0).get(3));
                    // 振幅
                    double zf = (high - low) / low;
                    if (zf > 0.03 && curVol > totalVolume / 2) {
                        result.add(instId);
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }
}
